﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace TestProject1
{
    class CommonUtility
    {

        public static void WriteLog(string message)
        {
            string filename = "c:\\temp\\log.txt";
            string content = string.Format("[{0}] {1}", DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"), message);
            using (System.IO.StreamWriter sw = new System.IO.StreamWriter(filename, true))
                sw.WriteLine(content);
        }


        public static void Sleep(int seconds)
        {
            System.Threading.Thread.Sleep(1000 * seconds);
        }

        public static void StartProcess(string CommandName)
        {
            Process p = new Process();
            p.StartInfo.FileName = CommandName;
            //p.StartInfo.RedirectStandardOutput = true;
            //p.StartInfo.UseShellExecute = true;
            p.Start();
        }

        public static void KillProcess(string processname)
        {
            Process[] pss = Process.GetProcesses();
            for (int i = 0; i < pss.Length ; i++)
            {
                if (pss[i].ProcessName.ToLower().Trim() == processname)
                {
                    pss[i].CloseMainWindow();
                }       
            }

            //while (true)
            {
                System.Threading.Thread.Sleep(1000);
                Process[] pss2 = Process.GetProcesses();
                for (int i = 0; i < pss2.Length; i++)
                {
                    if (pss2[i].ProcessName.ToLower().Trim() == processname)
                    {
                        pss2[i].Kill();
                    }
                }
            }

        }
    }
}
